import { request } from './index'
import type { 
  ImageModel, 
  PaginatedResponse, 
  ApiResponse,
  SearchCriteria,
  SearchSuggestion,
  TagCloudItem,
  TagModel
} from '@/types'

// 搜索相关API
export const searchApi = {
  // 高级搜索
  advancedSearch(criteria: SearchCriteria): Promise<ApiResponse<ImageModel[]>> {
    return request.post('/search/advanced', criteria)
  },

  // 多标签筛选
  filterByTags(params: {
    tags: string[]
    logic?: 'AND' | 'OR'
    page?: number
    size?: number
    sortBy?: string
    sortDir?: string
  }): Promise<ApiResponse<ImageModel[]>> {
    return request.get('/search/tags', { params })
  },

  // 获取热门标签
  getPopularTags(limit: number = 20): Promise<ApiResponse<Array<{
    tagName: string
    usageCount: number
  }>>> {
    return request.get('/search/popular-tags', { params: { limit } })
  },

  // 获取标签云
  getTagCloud(maxTags: number = 50): Promise<ApiResponse<TagCloudItem[]>> {
    return request.get('/search/tag-cloud', { params: { maxTags } })
  },

  // 按分类获取标签
  getTagsByCategory(): Promise<ApiResponse<Record<string, TagModel[]>>> {
    return request.get('/search/tags-by-category')
  },

  // 搜索建议（自动完成）
  getSearchSuggestions(query: string, limit: number = 10): Promise<ApiResponse<SearchSuggestion[]>> {
    return request.get('/search/suggestions', { params: { query, limit } })
  },

  // 获取相关标签
  getRelatedTags(tagName: string, limit: number = 10): Promise<ApiResponse<string[]>> {
    return request.get('/search/related-tags', { params: { tagName, limit } })
  },

  // 快速搜索
  quickSearch(params: {
    keyword: string
    tags?: string[]
    mimeTypes?: string[]
    page?: number
    size?: number
  }): Promise<ApiResponse<ImageModel[]>> {
    return request.get('/search/quick', { params })
  },

  // 按时间范围搜索
  searchByDateRange(params: {
    startDate: string
    endDate: string
    keyword?: string
    tags?: string[]
    page?: number
    size?: number
  }): Promise<ApiResponse<ImageModel[]>> {
    return request.get('/search/date-range', { params })
  },

  // 按文件大小范围搜索
  searchByFileSizeRange(params: {
    minSize?: number
    maxSize?: number
    keyword?: string
    tags?: string[]
    page?: number
    size?: number
  }): Promise<ApiResponse<ImageModel[]>> {
    return request.get('/search/file-size-range', { params })
  },

  // 智能关键词搜索
  intelligentSearch(params: {
    keyword: string
    page?: number
    size?: number
    sortBy?: string
    sortDir?: string
  }): Promise<ApiResponse<{
    success: boolean
    message: string
    keyword: string
    data: ImageModel[]
    totalResults: number
    currentPage: number
    totalPages: number
    pageSize: number
    hasNext: boolean
    hasPrevious: boolean
    suggestions?: SearchSuggestion[]
  }>> {
    return request.get('/search/intelligent', { params })
  },

  // 获取热门搜索词
  getHotKeywords(limit: number = 10): Promise<ApiResponse<string[]>> {
    return request.get('/search/hot-keywords', { params: { limit } })
  },

  // 记录搜索历史
  recordSearchHistory(keyword: string, userId?: string): Promise<ApiResponse> {
    return request.post('/search/history', { keyword, userId })
  },

  // 获取搜索统计信息
  getSearchStatistics(): Promise<ApiResponse<{
    popularTags: Array<{ tagName: string; usageCount: number }>
    hotKeywords: string[]
    categoryStats: Record<string, number>
  }>> {
    return request.get('/search/statistics')
  }
}